using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CodeProject.Data.Entity;

namespace InvoiceSample
{
	partial class SampleInvoiceContext
	{
		partial void OnContextCreated()
		{
			new OptimisticConcurrencyManager(this);
		}
	}

	[AssociationEndBehavior("Lines", Owned=true)]
	[OptimisticConcurrency("LastChanged", typeof(LocalDateTimeConcurrencyResolver))]
	partial class Invoice {

		/// <summary>
		/// TotalPrice, an additional derived property.
		/// </summary>
		public Decimal TotalPrice
		{
			get {
				Decimal totalPrice = 0m;
				foreach (InvoiceLine line in this.Lines)
				{
					if (line.LinePrice.HasValue)
						totalPrice += line.LinePrice.Value;
				}
				return totalPrice;
			}
		}

		public override string ToString()
		{
			return String.Format("Invoice {0} ({1})", this.Identifier, this.InvoiceDate);
		}
	}

	[OptimisticConcurrency("LastChanged", typeof(LocalDateTimeConcurrencyResolver))]
	partial class InvoiceLine {

		/// <summary>
		/// LinePrice, an additional derived property.
		/// </summary>
		public Decimal? LinePrice
		{
			get { return (this.Quantity.HasValue && this.UnitPrice.HasValue) ? (decimal?)((decimal)this.Quantity.Value * this.UnitPrice.Value) : null; }
		}

		public override string ToString()
		{
			return String.Format("InvoiceLine {0} ({1}:{2}, {3}x${4})", this.ID, this.Product.ID, this.Product.Label, this.Quantity, this.UnitPrice);
		}
	}

	[OptimisticConcurrency("LastChanged", typeof(LocalDateTimeConcurrencyResolver))]
	partial class Contact {

		public override string ToString()
		{
			return String.Format("{0} {0} ({1})", this.GetType().Name, this.ID, this.Name);
		}

	}

	[OptimisticConcurrency("LastChanged", typeof(LocalDateTimeConcurrencyResolver))]
	partial class Product {

		public override string ToString()
		{
			return String.Format("Product {0} ({1})", this.ID, this.Label);
		}

	}

	[OptimisticConcurrency("LastChanged", typeof(LocalDateTimeConcurrencyResolver))]
	partial class CatalogFolder
	{

		public override string ToString()
		{
			return String.Format("CatalogFolder {0} ({1})", this.ID, this.Name);
		}

	}
}
